<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column prop="id" label="ID" width="60">
			</el-table-column>
			<el-table-column prop="name" label="类型名称" width="180" sortable>
			</el-table-column>
			<el-table-column prop="description" label="描述" width="180" sortable>
			</el-table-column>
			<el-table-column prop="parent.name" label="上级类型" min-width="200" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize" :total="pageInfo.total" style="float:right;">
			</el-pagination>
		</el-col>

		<el-dialog title="新增或修改" :visible.sync="productTypeFormVisible" :close-on-click-modal="false">
			<el-form :model="productType" label-width="80px" :rules="productTypeFormRules" ref="productTypeForm">
				<el-form-item label="类型名称" prop="name">
					<el-input v-model="productType.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="描述" prop="description">
					<el-input v-model="productType.description" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="上级类型" prop="parent">
					<el-select v-model="productType.parent" clearable value-key="id" placeholder="请选择">
						<el-option v-for="t in types" :key="t.id" :label="t.name" :value="t">
							<span style="float: left">{{ t.name }}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ t.description }}</span>
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="productTypeFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="save" :loading="productTypeLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	export default {
		data() {
			return {
				//分页相关
				query:{
					currentPage:1,
					pageSize:10,
					keyword:''
				},
				pageInfo:{
					total:0,
					rows:[]
				},
				listLoading: false,
				sels: [],//列表选中列
				productTypeFormVisible: false,//新增界面是否显示
				productTypeLoading: false,
				productTypeFormRules: {
					key: [
						{ required: true, message: '请输入键', trigger: 'blur' }
					],
					value: [
						{ required: true, message: '请输入值', trigger: 'blur' }
					]
				},
				//新增界面数据
				productType: {
					id:null,
					name:'',
					description: '',
					parent: {}
				},
				types: []
			}
		},
		methods: {
			search(){
				//重置当前页为第一页
				this.query.currentPage =1;
				this.getProductTypes();
			},
			handleCurrentChange(val) {
				this.query.currentPage= val;
				this.getProductTypes();
			},
			//获取用户列表
			getProductTypes() {
				this.listLoading = true; //发起请求之前显示加载条
				//发起请求获取数据
				this.$http.post("/productType",this.query)
						.then(result =>{
							result = result.data; //{success,result:{}}
							if (result.success){
								this.pageInfo = result.data;
							}else{
								this.$message({
									message: '获取分页数据失败！'+result.message,
									type: 'error'
								});
							}
							this.listLoading = false;
						})
						.catch(result=>{
						this.$message({
								message: '系统错误！',
								type: 'error'
							});
						})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = row.id;
					this.$http.delete("/productType/" + para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getProductTypes();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.productTypeFormVisible = true;
				this.getTypes();
				this.productType = Object.assign({}, row); //回显

			},
			//显示新增界面
			handleAdd: function () {
				this.productTypeFormVisible = true;
				this.productType =   {
					id:null,
					name:'',
					description: '',
					parent: {}
				};
				this.getTypes();
			},

			getTypes() {
				this.$http.get("/productType")
				.then(result => {
					result = result.data;
					if (result.success) {
						this.types = result.data;
					}
				})
				.catch(result => {
					this.$message({
						message: '获取失败,' + result.message,
						type: 'error'
					});
				})
			},
			//编辑
			save: function () {
				this.$refs.productTypeForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.productTypeLoading = true;
							let para = Object.assign({}, this.productType);
							
							this.$http.put("/productType", para).then((res) => {
								res = res.data;
								if (res.success){
									this.$message({
										message: '保存成功',
										type: 'success'
									});

									this.productTypeFormVisible = false; //关闭对话框

									this.$refs['productTypeForm'].resetFields(); //重置表单

									this.getProductTypes(); //重新加载数据
								}else{
									this.$message({
										message: '保存失败,'+res.message,
										type: 'error'
									});
								}

								this.productTypeLoading = false; //关闭加载条
							});
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					
					this.$http.patch("/productType", ids).then((res) => {
						this.listLoading = false;
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getProductTypes();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getProductTypes();
		}
	}

</script>

<style scoped>

</style>